{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. shell.ipynb\n",
      "LICENSE\n",
      "README.md\n"
     ]
    }
   ],
   "source": [
    "$ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import (\n",
    "    \"bytes\"\n",
    "    \"fmt\"\n",
    "    \"log\"\n",
    "    \"os/exec\"\n",
    "    \"strings\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      ".\n",
      "..\n",
      "1. shell.ipynb\n",
      ".git\n",
      ".gitignore\n",
      ".ipynb_checkpoints\n",
      "LICENSE\n",
      "README.md\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "73 <nil>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cmd := exec.Command(\"ls\", \"-a\")\n",
    "\n",
    "var out bytes.Buffer\n",
    "cmd.Stdout = &out\n",
    "\n",
    "err := cmd.Run()\n",
    "if err != nil {\n",
    "    log.Fatal(err)\n",
    "}\n",
    "fmt.Printf(out.String())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "func run(command string) string {\n",
    "    args := strings.Split(command, \" \")\n",
    "    cmd := exec.Command(args[0], args[1:]...)\n",
    "    \n",
    "    var out bytes.Buffer\n",
    "    cmd.Stdout = &out\n",
    "    \n",
    "    cmd.Run()\n",
    "    \n",
    "    return out.String()\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1. shell.ipynb\n",
       "LICENSE\n",
       "README.md\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run(\"ls\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Linux pop-os 5.4.0-7642-generic #46~1598628707~20.04~040157c-Ubuntu SMP Fri Aug 28 18:02:16 UTC  x86_64 x86_64 x86_64 GNU/Linux\n"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run(\"uname -a\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "yingshaoxo :1           2020-11-15 08:59 (:1)\n"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "run(\"who\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Go",
   "language": "go",
   "name": "gophernotes"
  },
  "language_info": {
   "codemirror_mode": "",
   "file_extension": ".go",
   "mimetype": "",
   "name": "go",
   "nbconvert_exporter": "",
   "pygments_lexer": "",
   "version": "go1.15.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
